package com.taijihuabao.modules.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taijihuabao.common.exception.ServiceException;
import com.taijihuabao.common.jsonresult.ApplicationResponseCode;
import com.taijihuabao.modules.entity.PermissionEntity;
import com.taijihuabao.modules.mapper.PermissionMapper;
import com.taijihuabao.modules.model.permission.dto.PermissionCrFromDTO;
import com.taijihuabao.modules.model.permission.dto.PermissionListFromDTO;
import com.taijihuabao.modules.model.permission.dto.PermissionUpFromDTO;
import com.taijihuabao.modules.model.permission.vo.PermissionInfoVO;
import com.taijihuabao.modules.service.PermissionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 权限管理
 *
 * @author Wang
 */
@Service
public class PermissionServiceImpl implements PermissionService {

    @Resource
    PermissionMapper permissionMapper;

    @Override
    public PageInfo selectAllPermissions(PermissionListFromDTO listFromDTO) {
        PageHelper.startPage(listFromDTO.getPage(), listFromDTO.getSize());
        PermissionEntity permissionEntity = new PermissionEntity();
        permissionEntity.setName(listFromDTO.getName());
        List<PermissionEntity> permissionEntities = permissionMapper.selectAllPermissions(permissionEntity);
        PageInfo pageInfo = new PageInfo(permissionEntities);
        return pageInfo;
    }

    @Override
    public List<PermissionEntity> selectAll() {
        return permissionMapper.selectAll();
    }

    @Override
    public PermissionInfoVO selectById(Long id) {
        PermissionInfoVO permissionInfoVO = permissionMapper.selectById(id);
        if (permissionInfoVO == null) {
            throw new ServiceException(ApplicationResponseCode.PERMISSION_NOT_EXIST);
        } else {
            return permissionInfoVO;
        }
    }

    @Override
    public void insertPermission(PermissionCrFromDTO crFromDTO) {
        //判断权限编码、名字
        judgePermission(crFromDTO);
        PermissionEntity permissionEntity = new PermissionEntity();
        //新增权限信息
        permissionEntity.setCode(crFromDTO.getCode());
        permissionEntity.setName(crFromDTO.getName());
        permissionMapper.insertPermission(permissionEntity);
    }

    @Override
    public void updatePermission(PermissionUpFromDTO upFromDTO) {
        PermissionInfoVO permissionInfoVO = permissionMapper.selectById(upFromDTO.getId());
        //判断传入的权限id是否有误
        if (permissionInfoVO == null) {
            throw new ServiceException(ApplicationResponseCode.PERMISSION_NOT_ID);
        }
        //判断权限编码、名字
        judgePermission(upFromDTO);
        PermissionEntity permissionEntity = new PermissionEntity();
        //更新权限信息
        permissionEntity.setId(upFromDTO.getId());
        permissionEntity.setCode(upFromDTO.getCode());
        permissionEntity.setName(upFromDTO.getName());
        permissionMapper.updatePermission(permissionEntity);
    }

    @Override
    public void updateDelPermission(Long id) {
        PermissionEntity permissionEntity = new PermissionEntity();
        permissionEntity.setId(id);
        if (permissionMapper.selectDelPermission(id) != null) {
            permissionMapper.updateDelPermission(permissionEntity);
        } else {
            throw new ServiceException(ApplicationResponseCode.FAILED);
        }
    }

    @Override
    public void deletePermission(Long id) {
        PermissionEntity permissionEntity = new PermissionEntity();
        permissionEntity.setId(id);
        if (permissionMapper.selectById(id) != null) {
            permissionMapper.deletePermission(permissionEntity);
        } else {
            throw new ServiceException(ApplicationResponseCode.PERMISSION_DELETE_OR_NOT_EXIST);
        }
    }

    private void judgePermission(PermissionCrFromDTO crFromDTO) {
        //判断权限编码是否为空
        if (StringUtils.isEmpty(crFromDTO.getCode()) || "".equals(crFromDTO.getCode())) {
            throw new ServiceException(ApplicationResponseCode.PERMISSION_NOT_CODE);
        }
        //判断权限名称是否为空
        if (StringUtils.isEmpty(crFromDTO.getName()) || "".equals(crFromDTO.getName())) {
            throw new ServiceException(ApplicationResponseCode.PERMISSION_NOT_NAME);
        }
//        List<PermissionEntity> permissionEntities = permissionMapper.selectAll();
//        for (PermissionEntity permission : permissionEntities) {
//            //判断新增权限名是否重复
//            if (permission.getName().equals(crFromDTO.getName())) {
//                throw new ServiceException(ApplicationResponseCode.PERMISSION_NAME_IS_EXIST);
//            }
//            //判断新增权限编码是否重复
//            if (permission.getCode().equals(crFromDTO.getCode())) {
//                throw new ServiceException(ApplicationResponseCode.PERMISSION_CODE_IS_EXIST);
//            }
//        }
    }

}
